<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>Pandas数据规整-重塑和轴向旋转 | Python 数据分析学习目录</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../数据分析库的操作/13.1透视表和交叉表.html" />
    
    
    <link rel="prev" href="../数据分析库的操作/16Pandas数据规整-合并.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="4.5"
        data-chapter-title="Pandas数据规整-重塑和轴向旋转"
        data-filepath="数据分析库的操作/13Pandas数据规整-重塑和轴向旋转.md"
        data-basepath=".."
        data-revision="Wed Oct 24 2018 21:30:49 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        数据分析
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="Python数据分析序言/内容序言.html">
            
                
                    <a href="../Python数据分析序言/内容序言.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Python数据分析内容
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="python数据分析环境和工具/Python数据分析相关.html">
            
                
                    <a href="../python数据分析环境和工具/Python数据分析相关.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        python数据分析环境和工具
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="python数据分析环境和工具/1Python数据课程软件和环境安装.html">
            
                
                    <a href="../python数据分析环境和工具/1Python数据课程软件和环境安装.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        Python数据课程 软件和环境安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="python数据分析环境和工具/2python发行版.html">
            
                
                    <a href="../python数据分析环境和工具/2python发行版.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        python发行版
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="python数据分析环境和工具/5交互式编辑器-JupyterNotebook.html">
            
                
                    <a href="../python数据分析环境和工具/5交互式编辑器-JupyterNotebook.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        交互式编辑器-JupyterNotebook
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.3.1" data-path="python数据分析环境和工具/5.1Jupyter-notebook拓展应用.html">
            
                
                    <a href="../python数据分析环境和工具/5.1Jupyter-notebook拓展应用.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.1.</b>
                        
                        Jupyter-notebook拓展应用
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="python数据分析环境和工具/包和环境管理器：conda.html">
            
                
                    <a href="../python数据分析环境和工具/包和环境管理器：conda.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        包和环境管理器：conda
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.4.1" data-path="python数据分析环境和工具/pip和Virtualenv.html">
            
                
                    <a href="../python数据分析环境和工具/pip和Virtualenv.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.1.</b>
                        
                        pip和Virtualenv
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="python数据分析环境和工具/3Markdown.html">
            
                
                    <a href="../python数据分析环境和工具/3Markdown.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        标记语言：Markdown
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.5.1" data-path="python数据分析环境和工具/4Markdown语法.html">
            
                
                    <a href="../python数据分析环境和工具/4Markdown语法.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.1.</b>
                        
                        Markdown语法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5.2" data-path="python数据分析环境和工具/6Gitbook文档.html">
            
                
                    <a href="../python数据分析环境和工具/6Gitbook文档.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.2.</b>
                        
                        文档管理工具-Gitbook
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="数据分析库的初步认识/index.html">
            
                
                    <a href="../数据分析库的初步认识/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        数据分析库-Pandas
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="数据分析库的初步认识/Pandas创建.html">
            
                
                    <a href="../数据分析库的初步认识/Pandas创建.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        pandas
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="数据分析库的初步认识/Series创建.html">
            
                
                    <a href="../数据分析库的初步认识/Series创建.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        Series
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="数据分析库的初步认识/DataFrame创建.html">
            
                
                    <a href="../数据分析库的初步认识/DataFrame创建.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        DataFrame对象-创建
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" >
            
            <span><b>4.</b> 数据分析库的操作</span>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="数据分析库的操作/1DataFrame查询1-整体.html">
            
                
                    <a href="../数据分析库的操作/1DataFrame查询1-整体.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        DataFrame查询1-整体
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="数据分析库的操作/2DataFrame查询2-专用查询.html">
            
                
                    <a href="../数据分析库的操作/2DataFrame查询2-专用查询.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        DataFrame查询2-专用查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="数据分析库的操作/3DataFrame查询3-专有查询：过滤查询.html">
            
                
                    <a href="../数据分析库的操作/3DataFrame查询3-专有查询：过滤查询.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        DataFrame查询3-专有查询：过滤查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="数据分析库的操作/4Pandas对象的数据操作：增删改查.html">
            
                
                    <a href="../数据分析库的操作/4Pandas对象的数据操作：增删改查.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        Pandas对象的数据操作：增删改查
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.4.1" data-path="数据分析库的操作/5Pandas数据操作：其他操作.html">
            
                
                    <a href="../数据分析库的操作/5Pandas数据操作：其他操作.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.1.</b>
                        
                        Pandas数据操作：其他操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.2" data-path="数据分析库的操作/6Pandas数据存取.html">
            
                
                    <a href="../数据分析库的操作/6Pandas数据存取.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.2.</b>
                        
                        Pandas数据存取
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.3" data-path="数据分析库的操作/7Pandas数据运算.html">
            
                
                    <a href="../数据分析库的操作/7Pandas数据运算.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.3.</b>
                        
                        Pandas数据运算
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.4.3.1" data-path="数据分析库的操作/7.1Pandas数据运算拓展.html">
            
                
                    <a href="../数据分析库的操作/7.1Pandas数据运算拓展.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.3.1.</b>
                        
                        Pandas数据运算-拓展
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.4.4" data-path="数据分析库的操作/8Pandas分组聚合1.html">
            
                
                    <a href="../数据分析库的操作/8Pandas分组聚合1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.4.</b>
                        
                        Pandas分组聚合1
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.5" data-path="数据分析库的操作/9Pandas分组聚合2.html">
            
                
                    <a href="../数据分析库的操作/9Pandas分组聚合2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.5.</b>
                        
                        Pandas分组聚合2
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.6" data-path="数据分析库的操作/10Pandas数据规整-清理.html">
            
                
                    <a href="../数据分析库的操作/10Pandas数据规整-清理.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.6.</b>
                        
                        Pandas数据规整-清理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.7" data-path="数据分析库的操作/11Pandas数据规整-转换.html">
            
                
                    <a href="../数据分析库的操作/11Pandas数据规整-转换.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.7.</b>
                        
                        Pandas数据规整-转换
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.4.7.1" data-path="数据分析库的操作/14Pandas数据规整-转换-离散化和面元划分.html">
            
                
                    <a href="../数据分析库的操作/14Pandas数据规整-转换-离散化和面元划分.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.7.1.</b>
                        
                        离散化和面元划分
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.4.8" data-path="数据分析库的操作/16Pandas数据规整-合并.html">
            
                
                    <a href="../数据分析库的操作/16Pandas数据规整-合并.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.8.</b>
                        
                        Pandas数据规整-合并
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter active" data-level="4.5" data-path="数据分析库的操作/13Pandas数据规整-重塑和轴向旋转.html">
            
                
                    <a href="../数据分析库的操作/13Pandas数据规整-重塑和轴向旋转.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        Pandas数据规整-重塑和轴向旋转
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.5.1" data-path="数据分析库的操作/13.1透视表和交叉表.html">
            
                
                    <a href="../数据分析库的操作/13.1透视表和交叉表.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.1.</b>
                        
                        透视表和交叉表
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="Python可视化/绘图库-Matplotlib.html">
            
                
                    <a href="../Python可视化/绘图库-Matplotlib.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        Python可视化
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="Python可视化/绘图库-Matplotlib/Matplotlib常见图表.html">
            
                
                    <a href="../Python可视化/绘图库-Matplotlib/Matplotlib常见图表.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        基础：Matplotlib常见图表
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1.1" data-path="Python可视化/绘图库-Matplotlib/Matplotlib常见设置和操作.html">
            
                
                    <a href="../Python可视化/绘图库-Matplotlib/Matplotlib常见设置和操作.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.1.</b>
                        
                        Matplotlib常见设置和操作
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="Python可视化/绘图库-Matplotlib/1Matplotlib-绘图区域.html">
            
                
                    <a href="../Python可视化/绘图库-Matplotlib/1Matplotlib-绘图区域.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        提升：绘图区域
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="Python可视化/绘图库-Matplotlib/2Matplotlib-图像组件.html">
            
                
                    <a href="../Python可视化/绘图库-Matplotlib/2Matplotlib-图像组件.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        提升：绘图组件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="Python可视化/绘图库-Matplotlib/3Matplotlib-高级绘图.html">
            
                
                    <a href="../Python可视化/绘图库-Matplotlib/3Matplotlib-高级绘图.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        拓展：高级绘图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="Python可视化/绘图库-Matplotlib/4数学计算展示图像.html">
            
                
                    <a href="../Python可视化/绘图库-Matplotlib/4数学计算展示图像.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        拓展：数学计算展示图像
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="Python可视化/绘图库-Matplotlib/5注意事项.html">
            
                
                    <a href="../Python可视化/绘图库-Matplotlib/5注意事项.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        拓展：注意事项
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="Python可视化/绘图库-Matplotlib/6pylab.html">
            
                
                    <a href="../Python可视化/绘图库-Matplotlib/6pylab.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        拓展：pylab
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="数据分析必备知识点/数据分析必备知识点汇集.html">
            
                
                    <a href="../数据分析必备知识点/数据分析必备知识点汇集.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        数据分析必备知识点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="数据分析必备知识点/数据分析流程.html">
            
                
                    <a href="../数据分析必备知识点/数据分析流程.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        数据分析流程
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >Python 数据分析学习目录</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="pandas&#x6570;&#x636E;&#x89C4;&#x6574;&#x91CD;&#x5851;&#x548C;&#x8F74;&#x5411;&#x65CB;&#x8F6C;">Pandas&#x6570;&#x636E;&#x89C4;&#x6574;-&#x91CD;&#x5851;&#x548C;&#x8F74;&#x5411;&#x65CB;&#x8F6C;</h1>
<hr>
<p>&#x6709;&#x8BB8;&#x591A;&#x7528;&#x4E8E;&#x91CD;&#x65B0;&#x6392;&#x5217;&#x8868;&#x683C;&#x578B;&#x6570;&#x636E;&#x7684;&#x57FA;&#x7840;&#x8FD0;&#x7B97;&#x3002;&#x8FD9;&#x4E9B;&#x51FD;&#x6570;&#x4E5F;&#x79F0;&#x4F5C;&#x91CD;&#x5851;&#xFF08;reshape&#xFF09;&#x6216;&#x8F74;&#x5411;&#x65CB;&#x8F6C;&#xFF08;pivot&#xFF09;&#x8FD0;&#x7B97;&#x3002;</p>
<p>&#x6570;&#x636E;&#x91CD;&#x5851;&#x548C;&#x8F74;&#x5411;&#x65CB;&#x8F6C;&#x64CD;&#x4F5C;&#xFF1A;&#x8868;&#x793A;&#x8F6C;&#x6362;&#x4E00;&#x4E2A;&#x8868;&#x683C;&#x6216;&#x5411;&#x91CF;&#x7684;&#x7ED3;&#x6784;&#xFF0C;&#x4F7F;&#x5176;&#x9002;&#x5408;&#x4E8E;&#x8FDB;&#x4E00;&#x6B65;&#x7684;&#x5206;&#x6790;</p>
<h2 id="&#x91CD;&#x5851;&#x5C42;&#x6B21;&#x5316;&#x7D22;&#x5F15;">&#x91CD;&#x5851;&#x5C42;&#x6B21;&#x5316;&#x7D22;&#x5F15;</h2>
<hr>
<p>&#x5C42;&#x6B21;&#x5316;&#x7D22;&#x5F15;&#x4E3A;DataFrame&#x6570;&#x636E;&#x7684;&#x91CD;&#x6392;&#x4EFB;&#x52A1;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x79CD;&#x5177;&#x6709;&#x826F;&#x597D;&#x4E00;&#x81F4;&#x6027;&#x7684;&#x65B9;&#x5F0F;&#x3002;&#x4E3B;&#x8981;&#x529F;&#x80FD;&#x6709;&#x4E8C;&#xFF1A;</p>
<p>stack&#xFF1A;&#x5217;&#x8F6C;&#x884C;&#xFF0C;&#x5C06;&#x6570;&#x636E;&#x7684;&#x5217;&#x201C;&#x65CB;&#x8F6C;&#x201D;&#x4E3A;&#x884C;</p>
<pre><code>&#xFF08;&#x5C11;&#x7528;&#x4E00;&#x822C;&#x7528;&#x4E8E;&#x5C06;DataFrame&#x8F6C;&#x4E3A;&#x5C42;&#x6B21;&#x5316;Series&#x7528;&#xFF09;
</code></pre><p><strong> unstack </strong>&#xFF1A;&#x884C;&#x8F6C;&#x5217;&#xFF0C;&#x5C06;&#x6570;&#x636E;&#x7684;&#x884C;&#x201C;&#x65CB;&#x8F6C;&#x201D;&#x4E3A;&#x5217; </p>
<pre><code>&#xFF08;&#x5E38;&#x7528;&#xFF1A;&#x4E00;&#x822C;&#x7528;&#x4E8E;&#x5C06;&#x5C42;&#x6B21;&#x5316;Series&#x8F6C;&#x4E3A;DataFrame&#xFF09;

&#x4E8C;&#x8005;&#x4E92;&#x4E3A;&#x9006;&#x8FD0;&#x7B97;
</code></pre><p> Series &#x6CA1;&#x6709;stack&#x5C5E;&#x6027;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">import</span> pandas <span class="hljs-keyword">as</span> pd
</code></pre>
<pre><code class="lang-python"><span class="hljs-comment"># &#x4E00;&#x4E2A;&#x884C;&#x5217;&#x7D22;&#x5F15;&#x90FD;&#x5E26;name&#x7684;DataFrame&#x5BF9;&#x8C61;</span>
data = pd.DataFrame(
    np.arange(<span class="hljs-number">6</span>).reshape((<span class="hljs-number">2</span>, <span class="hljs-number">3</span>)),
    index=pd.Index([<span class="hljs-string">&apos;Ohio&apos;</span>,<span class="hljs-string">&apos;Colorado&apos;</span>], name=<span class="hljs-string">&apos;state&apos;</span>),
    columns=pd.Index([<span class="hljs-string">&apos;one&apos;</span>, <span class="hljs-string">&apos;two&apos;</span>, <span class="hljs-string">&apos;three&apos;</span>], name=<span class="hljs-string">&apos;number&apos;</span>)
)
data
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th>number</th>
      <th>one</th>
      <th>two</th>
      <th>three</th>
    </tr>
    <tr>
      <th>state</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>Ohio</th>
      <td>0</td>
      <td>1</td>
      <td>2</td>
    </tr>
    <tr>
      <th>Colorado</th>
      <td>3</td>
      <td>4</td>
      <td>5</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python">data.index
</code></pre>
<pre><code>Index([&apos;Ohio&apos;, &apos;Colorado&apos;], dtype=&apos;object&apos;, name=&apos;state&apos;)
</code></pre><pre><code class="lang-python">data.columns
</code></pre>
<pre><code>Index([&apos;one&apos;, &apos;two&apos;, &apos;three&apos;], dtype=&apos;object&apos;, name=&apos;number&apos;)
</code></pre><h2 id="stack&#xFF1A;&#x5217;&#x8F6C;&#x884C;&#xFF0C;&#x5C06;&#x6570;&#x636E;&#x7684;&#x5217;&#x65CB;&#x8F6C;&#x4E3A;&#x884C;&#x7D22;&#x5F15;">stack&#xFF1A;&#x5217;&#x8F6C;&#x884C;&#xFF0C;&#x5C06;&#x6570;&#x636E;&#x7684;&#x5217;&#x201C;&#x65CB;&#x8F6C;&#x201D;&#x4E3A;&#x884C;&#x7D22;&#x5F15;</h2>
<p>&#x5217;&#x7D22;&#x5F15;&#xFF0C;&#x9006;&#x65F6;&#x9488;&#x8F6C;&#x4E3A;&#x884C;&#x7D22;&#x5F15;</p>
<pre><code class="lang-python">data
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th>number</th>
      <th>one</th>
      <th>two</th>
      <th>three</th>
    </tr>
    <tr>
      <th>state</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>Ohio</th>
      <td>0</td>
      <td>1</td>
      <td>2</td>
    </tr>
    <tr>
      <th>Colorado</th>
      <td>3</td>
      <td>4</td>
      <td>5</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python"><span class="hljs-comment"># &#x5217;&#x8F6C;&#x884C;&#xFF0C;stack&#x65B9;&#x6CD5;&#x5C06;&#x5217;&#x8F6C;&#x6362;&#x4E3A;&#x884C;&#xFF0C;&#x5F97;&#x5230;&#x4E00;&#x4E2A;Series</span>
result = data.stack()
result
</code></pre>
<pre><code>state     number
Ohio      one       0
          two       1
          three     2
Colorado  one       3
          two       4
          three     5
dtype: int32
</code></pre><h2 id="unstack&#xFF1A;&#x884C;&#x8F6C;&#x5217;&#xFF0C;&#x5C06;&#x6570;&#x636E;&#x7684;&#x884C;&#x65CB;&#x8F6C;&#x4E3A;&#x5217;">unstack&#xFF1A;&#x884C;&#x8F6C;&#x5217;&#xFF0C;&#x5C06;&#x6570;&#x636E;&#x7684;&#x884C;&#x201C;&#x65CB;&#x8F6C;&#x201D;&#x4E3A;&#x5217;</h2>
<p> &#x5BF9;&#x5C42;&#x6B21;&#x5316;&#x7D22;&#x5F15;&#x7684;Series&#xFF0C;&#x53EF;&#x4EE5;&#x7528;unstack&#x5C06;&#x5176;&#x91CD;&#x6392;&#x4E3A;&#x4E00;&#x4E2A;DataFrame</p>
<pre><code class="lang-python">result.unstack()
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th>number</th>
      <th>one</th>
      <th>two</th>
      <th>three</th>
    </tr>
    <tr>
      <th>state</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>Ohio</th>
      <td>0</td>
      <td>1</td>
      <td>2</td>
    </tr>
    <tr>
      <th>Colorado</th>
      <td>3</td>
      <td>4</td>
      <td>5</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python">data.unstack()  <span class="hljs-comment">#&#x5148;&#x628A;&#x822A;&#x7D22;&#x5F15;&#x987A;&#x65F6;&#x9488;&#x65CB;&#x8F6C;&#x63D2;&#x5165;&#x539F;&#x6765;&#x7684;&#x5217;&#x7D22;&#x5F15;&#xFF0C;&#x7136;&#x540E;&#x518D;&#x5C06;&#x6574;&#x4E2A;Series&#x9006;&#x65F6;&#x9488;&#x65CB;&#x8F6C;&#xFF0C;&#x6454;&#x5012;&#xFF0C;&#x7ED3;&#x679C;</span>
</code></pre>
<pre><code>number  state   
one     Ohio        0
        Colorado    3
two     Ohio        1
        Colorado    4
three   Ohio        2
        Colorado    5
dtype: int32
</code></pre><pre><code class="lang-python"><span class="hljs-comment"># result.stack()   #&#x62A5;&#x9519;&#x3002;Series  &#x5BF9;&#x8C61;&#x6CA1;&#x6709;&#x5217;&#xFF0C;&#x6240;&#x4EE5;&#x6CA1;&#x6709;Stack&#xFF08;&#xFF09; &#x65B9;&#x6CD5;</span>
</code></pre>
<pre><code class="lang-python">result
</code></pre>
<pre><code>state     number
Ohio      one       0
          two       1
          three     2
Colorado  one       3
          two       4
          three     5
dtype: int32
</code></pre><h3 id="unstack&#x9ED8;&#x8BA4;&#x64CD;&#x4F5C;&#x7684;&#x662F;&#x6700;&#x5185;&#x5C42;&#xFF08;stack&#x4E5F;&#x662F;&#x5982;&#x6B64;&#xFF09;&#x3002;&#x4F20;&#x5165;&#x5206;&#x5C42;&#x7EA7;&#x522B;&#x7684;&#x7F16;&#x53F7;&#x6216;name&#x5373;&#x53EF;&#x5BF9;&#x5176;&#x5B83;&#x7EA7;&#x522B;&#x8FDB;&#x884C;unstack&#x64CD;&#x4F5C;">unstack&#x9ED8;&#x8BA4;&#x64CD;&#x4F5C;&#x7684;&#x662F;&#x6700;&#x5185;&#x5C42;&#xFF08;stack&#x4E5F;&#x662F;&#x5982;&#x6B64;&#xFF09;&#x3002;&#x4F20;&#x5165;&#x5206;&#x5C42;&#x7EA7;&#x522B;&#x7684;&#x7F16;&#x53F7;&#x6216;name&#x5373;&#x53EF;&#x5BF9;&#x5176;&#x5B83;&#x7EA7;&#x522B;&#x8FDB;&#x884C;unstack&#x64CD;&#x4F5C;</h3>
<pre><code class="lang-python">result
</code></pre>
<pre><code>state     number
Ohio      one       0
          two       1
          three     2
Colorado  one       3
          two       4
          three     5
dtype: int32
</code></pre><pre><code class="lang-python">result.unstack()   <span class="hljs-comment">#&#x9ED8;&#x8BA4;&#x65CB;&#x8F6C;&#x6700;&#x91CC;&#x5C42;&#x7D22;&#x5F15;</span>
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th>number</th>
      <th>one</th>
      <th>two</th>
      <th>three</th>
    </tr>
    <tr>
      <th>state</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>Ohio</th>
      <td>0</td>
      <td>1</td>
      <td>2</td>
    </tr>
    <tr>
      <th>Colorado</th>
      <td>3</td>
      <td>4</td>
      <td>5</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python">result.unstack(<span class="hljs-number">0</span>)   <span class="hljs-comment">#&#x65CB;&#x8F6C;&#x7B2C;0&#x5C42;&#x7D22;&#x5F15;&#xFF0C;&#x6700;&#x5916;&#x5C42;</span>
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th>state</th>
      <th>Ohio</th>
      <th>Colorado</th>
    </tr>
    <tr>
      <th>number</th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>one</th>
      <td>0</td>
      <td>3</td>
    </tr>
    <tr>
      <th>two</th>
      <td>1</td>
      <td>4</td>
    </tr>
    <tr>
      <th>three</th>
      <td>2</td>
      <td>5</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python">result.unstack(<span class="hljs-string">&apos;state&apos;</span>)  <span class="hljs-comment"># &#x5199;&#x7D22;&#x5F15;name&#x4E5F;&#x884C;</span>
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th>state</th>
      <th>Ohio</th>
      <th>Colorado</th>
    </tr>
    <tr>
      <th>number</th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>one</th>
      <td>0</td>
      <td>3</td>
    </tr>
    <tr>
      <th>two</th>
      <td>1</td>
      <td>4</td>
    </tr>
    <tr>
      <th>three</th>
      <td>2</td>
      <td>5</td>
    </tr>
  </tbody>
</table>
</div>




<pre><code class="lang-python">data.unstack().unstack()  <span class="hljs-comment">#&#x53D8;&#x4E3A;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x8868;&#xFF0C;&#x4F46;&#x662F;&#x8FD9;&#x4E2A;&#x65F6;&#x5019;&#x662F;&#x5C06;&#x539F;&#x8868;&#x884C;&#x548C;&#x5217;&#x4EA4;&#x6362;</span>
</code></pre>
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th>state</th>
      <th>Ohio</th>
      <th>Colorado</th>
    </tr>
    <tr>
      <th>number</th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>one</th>
      <td>0</td>
      <td>3</td>
    </tr>
    <tr>
      <th>two</th>
      <td>1</td>
      <td>4</td>
    </tr>
    <tr>
      <th>three</th>
      <td>2</td>
      <td>5</td>
    </tr>
  </tbody>
</table>
</div>



<p>&#x5C0F;&#x603B;&#x7ED3;&#xFF1A;</p>
<ul>
<li>&#x53EF;&#x4EE5;&#x5C06;&#x4E00;&#x4E2A;DataFrame&#x7C7B;&#x578B;&#x7684;&#x5C42;&#x6B21;&#x5316;&#x7D22;&#x5F15;&#x8FDB;&#x884C;stack&#x6216;&#x8005;unstack<ul>
<li>data.stack()</li>
<li>data.unstack()</li>
</ul>
</li>
<li>&#x4F46;&#x662F;&#x4E0D;&#x80FD;&#x5C06;&#x5176;stack&#x6216;&#x8005;unstack&#x4E4B;&#x540E;&#x5F97;&#x5230;&#x7684;Series&#x7C7B;&#x578B;&#x8FDB;&#x884C;stack&#x64CD;&#x4F5C;<ul>
<li>data.stack&#xFF08;&#xFF09;&#x4E4B;&#x540E;<ul>
<li>data.stack().unstack()&#x5373;&#x4E3A;&#x6062;&#x590D;&#x539F;&#x683C;&#x5F0F;</li>
<li>data.stack().stack() &#x4E0D;&#x80FD;&#x8FDB;&#x884C;&#x6B64;&#x64CD;&#x4F5C;&#xFF0C;&#x4F1A;&#x62A5;&#x9519;</li>
</ul>
</li>
<li>data.unstack()&#x4E4B;&#x540E;<ul>
<li>data.unstack().unstack()&#x53D8;&#x4E3A;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x8868;&#xFF0C;&#x5176;&#x5B9E;&#x5C31;&#x662F;&#x5C06;&#x539F;&#x8868;&#x884C;&#x548C;&#x5217;&#x4EA4;&#x6362;</li>
<li>data.unstack().stack() &#x4E0D;&#x80FD;&#x8FDB;&#x884C;&#x6B64;&#x64CD;&#x4F5C;&#xFF0C;&#x4F1A;&#x62A5;&#x9519;</li>
</ul>
</li>
</ul>
</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../数据分析库的操作/16Pandas数据规整-合并.html" class="navigation navigation-prev " aria-label="Previous page: Pandas数据规整-合并"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../数据分析库的操作/13.1透视表和交叉表.html" class="navigation navigation-next " aria-label="Next page: 透视表和交叉表"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
